local p = {}
-- 通用
local getArgs = require('Module:Arguments').getArgs
local inArray = require('Module:Util').inArray
local OrderedTable = require('Module:OrderedTable')
local summon_map = require('Module:Util/Summon')

---------------------------------------------------------------------------
-- 获取数据
---------------------------------------------------------------------------

function p.query_data(rarity)
	-- 整理数据
	local output = {}

	for _, card_info in pairs(summon_map) do
	    card_info.ID = _
		if (tonumber(card_info.ID) or 0) > 0 and (card_info.addDate or '') ~= '' then
		    if (rarity or '') == '' or (card_info.rarity == rarity) then
				table.insert(output, card_info)
			end
		end
	end
	
	table.sort(output, p.sort_summon_data)

	return output
end

function p.sort_summon_data(a, b)
    if (a.addDate ~= b.addDate) then
		return a.addDate < b.addDate
	elseif (a.series ~= b.series) then
		return a.series < b.series
	elseif (a.element ~= b.element) then
		return a.element < b.element
	else
		return a.ID < b.ID
	end
end

function p.reverseTable(tab)
	local tmp = {}
	for i = 1, #tab do
		local key = #tab
		tmp[i] = table.remove(tab)
	end

	return tmp
end

-----------------------------------------------------------------------------
-- 回响信息页生成
-----------------------------------------------------------------------------

function p.summon_list(frame)
	local args = getArgs(frame)

	local rarity = args[1] or ''
	local picture_l = picture
	local bool = true
	local summon_data = p.query_data(rarity)

	local html = mw.html.create()

	if #summon_data > 0 then
		local flex_list = html:tag('div'):addClass('flex-list m-flex-list'):cssText('text-align: center;font-size:14px;')

		for _, card_info in ipairs(summon_data) do
			local link = 'summon/'..card_info.ID
			flex_list:tag('div'):addClass('flex-item summon-box'):tag('div'):addClass('image'):wikitext('[[File:s_'..card_info.ID..'.png|link='..link..']]'):done()
			:tag('div'):addClass('text'):wikitext('[['..link..'|'..(card_info.tag_title or card_info.name)..']]'):done()
		end
	end

	return html

	-- body
end


return p
